<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\exam\logic\admin\PaperCategory as CategoryLogic;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 题库分类-后台
 */
class PaperCategory extends BaseService
{
    /**
     * @Inject()
     *
     * @var CategoryLogic
     */
    protected $categoryLogic;
    /**
     * 分类顶级数据列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @return mixed
     */
    public function getList()
    {
        $data = $this->categoryLogic->getList();
        // 定义字段
        $keyList = [
            'title' => ListItem::make()->title('名称'),
            'num'   => ListItem::make()->title('试卷数量'),
        ];

        // 设置表单
        $list = new PageList;
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 分类子集
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @param $id
     * @return \think\Response
     */
    public function getChildren(int $id)
    {
        $data = $this->categoryLogic->getList($id);

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * 创建分类
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @return \think\Response
     */
    public function createCategory()
    {
        $data = Request::post();
        $re   = $this->categoryLogic->createCategory($data);

        return $this->parseToData($re, 1, '创建成功');
    }

    /**
     * 编辑分类
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @param $id
     * @return \think\Response
     */
    public function editCategory(int $id)
    {
        $data = Request::put();
        $re   = $this->categoryLogic->editCategory($id, $data);

        return $this->parseToData($re, 1, '编辑成功');
    }

    /**
     * 删除分类
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @param $id
     * @return \think\Response
     */
    public function deleteCategory(int $id)
    {
        $this->categoryLogic->deleteCategory($id);

        return $this->parseToData([], 1, '删除成功');
    }
}
